package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.po.PoctInstrument;
import com.rzdata.poct.system.domain.business.bo.PoctInstrumentBo;
import com.rzdata.poct.system.domain.business.vo.PoctInstrumentVo;
import com.rzdata.poct.system.mapper.PoctInstrumentMapper;
import com.rzdata.poct.system.service.IPoctInstrumentService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * POCT检测设备Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctInstrumentServiceImpl extends ServicePlusImpl<PoctInstrumentMapper, PoctInstrument, PoctInstrumentVo> implements IPoctInstrumentService {

    @Override
    public PoctInstrumentVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctInstrumentVo> queryPageList(PoctInstrumentBo bo) {
        PagePlus<PoctInstrument, PoctInstrumentVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctInstrumentVo> queryList(PoctInstrumentBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctInstrument> buildQueryWrapper(PoctInstrumentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctInstrument> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentNo()), PoctInstrument::getInstrumentNo, bo.getInstrumentNo());
        lqw.like(StringUtils.isNotBlank(bo.getInstrumentName()), PoctInstrument::getInstrumentName, bo.getInstrumentName());
        lqw.eq(bo.getDeptId() != null, PoctInstrument::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getInstrumentModel()), PoctInstrument::getInstrumentModel, bo.getInstrumentModel());
        lqw.eq(StringUtils.isNotBlank(bo.getManufacturer()), PoctInstrument::getManufacturer, bo.getManufacturer());
        lqw.eq(StringUtils.isNotBlank(bo.getMethodology()), PoctInstrument::getMethodology, bo.getMethodology());
        lqw.eq(StringUtils.isNotBlank(bo.getRegInfo()), PoctInstrument::getRegInfo, bo.getRegInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), PoctInstrument::getPic, bo.getPic());
        lqw.eq(StringUtils.isNotBlank(bo.getManager()), PoctInstrument::getManager, bo.getManager());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonLiable()), PoctInstrument::getPersonLiable, bo.getPersonLiable());
        lqw.eq(StringUtils.isNotBlank(bo.getDcf()), PoctInstrument::getDcf, bo.getDcf());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctInstrumentBo bo) {
        PoctInstrument add = BeanUtil.toBean(bo, PoctInstrument.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctInstrumentBo bo) {
        PoctInstrument update = BeanUtil.toBean(bo, PoctInstrument.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctInstrument entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
